<?php

/**
 *   OPENSHOP 管理中心期刊页码管理
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: catalog_pageno.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

$exc = new exchange($os->table("catalog_page_no"), $db, 'catalog_id', 'intro');

/*------------------------------------------------------ */
//-- 期刊列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{	
	admin_priv('view_catalog_pageno');

    $smarty->assign('ur_here',      '期刊页码列表');
    $smarty->assign('action_link',  array('text' => '添加期刊页码', 'href' => 'catalog_pageno.php?act=add&id='.$_GET['id']));
    $smarty->assign('full_page',    1);
    
    $catalog_pageno_list = get_catalog_pageno($_GET['id']);

    $smarty->assign('catalog_list', $catalog_pageno_list['catalog_pageno']);
    $smarty->assign('filter',       $catalog_pageno_list['filter']);
    $smarty->assign('record_count', $catalog_pageno_list['record_count']);
    $smarty->assign('page_count',   $catalog_pageno_list['page_count']);

    assign_query_info();
    
    $smarty->display('catalog_pageno_list.tpl');
    
}elseif($_REQUEST['act']=='add'){
	
    admin_priv('add_catalog_pageno');

    $smarty->assign('form_act', 'insert');
    
    if (empty($_GET['id']))
    {
        sys_msg('invalid param');
    }
    
    $catalog_id = intval($_GET['id']);
    $sql = "SELECT catalog_id,catalog_name FROM " . $GLOBALS['os']->table('catalog') .
            " WHERE catalog_id = '$catalog_id'";
    $catalog = $GLOBALS['db']->getRow($sql);
    
    if (empty($catalog))
    {
        sys_msg('您要操作的目录期刊不存在');
    }
    
    $smarty->assign('catalog_pageno', $catalog);
    
    $smarty->assign('ur_here', '添加目录期刊页码');
    
    $smarty->assign('action_link', array('href' => 'catalog_pageno.php?act=list&id='.$catalog_id, 'text' => '目录期刊页码管理'));
        
	assign_query_info();
	
	$smarty->display('add_catalog_pageno.tpl');	
	
}elseif ($_REQUEST['act'] == 'insert'){
	
    /* 检查权限 */
 	admin_priv('add_catalog_pageno');

    /* 是否添加 */
    $catalog_p_no = array();
    $catalog_p_no = $_POST['catalog_p_no'];
    for($i=0;$i<sizeof($catalog_p_no);$i++){
    	$c_p_no = $db->getOne("SELECT catalog_p_no FROM ".$os->table('catalog_page_no')." WHERE catalog_id=".
    	                            intval($_POST['catalog_id'])." AND catalog_p_no=".intval($catalog_p_no[$i]));
    	if(!empty($c_p_no) && $c_p_no>0){
        	sys_msg('目录期刊页码'.$catalog_p_no[$i].'已经存在', 0, $links);
    	}
    	$db->query("INSERT INTO ".$os->table('catalog_page_no')." VALUES(".intval($_POST['catalog_id']).
    	           ",".intval($catalog_p_no[$i]).",'".$_POST['intro'][$i]."')");
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    $links = array(
        array('href' => 'catalog_pageno.php?act=add&id='.intval($_POST['catalog_id']), 'text' => '继续添加目录期刊页码'),
        array('href' => 'catalog_pageno.php?act=list&id='.intval($_POST['catalog_id']), 'text' => '返回目录期刊页码列表')
    );
    sys_msg('添加目录期刊页码成功', 0, $links);
    
}elseif ($_REQUEST['act'] == 'remove'){	
    /* 检查权限 */
    admin_priv('drop_catalog_pageno');    

    $sql = "SELECT catalog_name FROM " . $os->table('catalog') . " WHERE catalog_id = " . intval($_GET['id']);
    $catalog_name = $db->getOne($sql);
    /* 通过插件来删除用户 */
    $db->query("DELETE FROM " . $os->table('catalog_page_no') . " WHERE catalog_id = " . intval($_GET['id']) . " AND catalog_p_no=".intval($_GET['p_no']));

    /* 记录管理员操作 */
    admin_log(addslashes($catalog_name.'-第'.intval($_GET['p_no']).'页'), 'remove', 'catalog_pageno');

    /* 提示信息 */
    $link[] = array('text' => '返回上一页', 'href'=>'catalog_pageno.php?act=list&id=' . intval($_GET['id']));
    sys_msg(sprintf('期刊页码 %s 已经删除成功。', $catalog_name.'-第'.intval($_GET['p_no']).'页'), 0, $link);
    
}elseif ($_REQUEST['act'] == 'query'){
	
    $list = get_catalog_pageno($_GET['id']);

    $smarty->assign('catalog_list',    $list['catalog_pageno']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    make_json_result($smarty->fetch('catalog_pageno_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));	
}elseif ($_REQUEST['act'] == 'add_goods'){

    /* 检查权限 */
    admin_priv(); 
    
	$res = $db->getAll("SELECT catalog_id,catalog_name FROM ".$os->table('catalog'));
	
    $catalog_list = array();
    foreach ($res AS $row)
    {
        $catalog_list[$row['catalog_id']] = $row['catalog_name'];
    }
	$smarty->assign('catalog_list',$catalog_list);
	$smarty->assign('form_act','insert_goods');
	
	assign_query_info();

	$smarty->display('catalog_pageno_addgoods.tpl');
}elseif($_REQUEST['act']=='insert_goods'){
	if(empty($_POST['goods_ids'])){
        sys_msg('invalid param');		
	}
	$goods_ids = explode(',',$_POST['goods_ids']);
	foreach($goods_ids as $goods_id){
		$catalog_id = $db->getOne("SELECT catalog_id FROM ".$os->table('catalog_p_no_goods')." WHERE catalog_id=".
		           intval($_POST['catalog_id'])." AND catalog_p_no=".intval($_POST['catalog_p_no']).
		           " AND goods_id=".intval($goods_id));
		if(!empty($catalog_id) || $catalog_id>0){
			$goods_name = $db->getOne("SELECT goods_name FROM ".$os->table('goods')." WHERE goods_id=".intval($goods_id));
   			sys_msg(sprintf('期刊%s页码商品已经添加',$goods_name));			
		}
		$db->query("INSERT INTO ".$os->table('catalog_p_no_goods')." VALUES(".intval($_POST['catalog_id']).",".intval($_POST['catalog_p_no']).",".intval($goods_id).")");
	}
    /* 显示提示信息，返回商品列表 */
    $link[] = array('href' => 'catalog_pageno.php?act=add_goods', 'text' => '继续期刊页码商品添加');
    sys_msg('期刊页码商品添加成功', 0, $link);
}elseif($_REQUEST['act'] == 'get_goods_list'){
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;

    $filters = $json->decode($_GET['JSON']);

    $arr = get_goods_list($filters);
    $opt = array();

    foreach ($arr AS $key => $val)
    {
        $opt[] = array('value' => $val['goods_id'],
                       'text'  => $val['goods_name'],
                       'data'  => '');
    }

    make_json_result($opt);
}elseif($_REQUEST['act'] == 'get_catalog_pageno_list'){
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;

    $catalog_id = intval($_GET['catalog_id']);

    $arr = $db->getAll("SELECT * FROM ".$os->table('catalog_page_no')." WHERE catalog_id=".$catalog_id);
    $opt = array();

    foreach ($arr AS $key => $val)
    {
        $opt[] = array('value' => $val['catalog_p_no'],
                       'text'  => '第'.$val['catalog_p_no'].'页',
                       'data'  => '');
    }

    make_json_result($opt);	
}elseif($_REQUEST['act'] == 'add_catalog_pageno_goods'){
	
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $fittings   = $json->decode($_GET['add_ids']);	
    $arr = $db->getAll("SELECT goods_id,goods_name FROM ".$os->table('goods')." WHERE ".db_create_in($fittings,'goods_id'));
    
    $opt = array();

    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['goods_id'],
                       'text'       => $val['goods_name'],
                       'data'       => '');
    }

    clear_cache_files();
    make_json_result($opt);   	
}

function get_catalog_pageno($catalog_id){
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'catalog_p_no' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'ASC' : trim($_REQUEST['sort_order']);

        $where = " AND catalog_id=".intval($catalog_id);
        if (!empty($filter['keyword']))
        {
            $where .= " AND catalog_p_no LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
        }

        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('catalog_page_no') .
                " WHERE 1 $where";
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * ".
                "FROM " . $GLOBALS['os']->table('catalog_page_no') .
                " WHERE 1 $where ".
                " ORDER BY $filter[sort_by] $filter[sort_order] ".
                " LIMIT ". $filter['start'] .", $filter[page_size]";

        $filter['keyword'] = stripslashes($filter['keyword']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);

    $list = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {   
    	$row_2 = $GLOBALS['db']->getRow("SELECT catalog_name FROM ".$GLOBALS['os']->table('catalog')." WHERE catalog_id=".intval($row['catalog_id']));
    	$row['catalog_name'] = $row_2['catalog_name'];
        $list[] = $row;
    }

    return array('catalog_pageno' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}
?>